package com.majun.huaxishe.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.majun.huaxishe.entity.Album;
import com.majun.huaxishe.entity.dto.AlbumDTO;
import com.majun.huaxishe.entity.vo.AlbumVO;
import com.majun.huaxishe.result.Result;
import com.majun.huaxishe.service.IAlbumService;
import com.majun.huaxishe.utils.HxsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 相册表 前端控制器
 * </p>
 *
 * @author 马骏
 * @since 2024-02-29
 */
@RestController
@RequestMapping("/album")
@CrossOrigin
public class AlbumController {

    @Autowired
    private IAlbumService albumService;

    @GetMapping("/queryPage")
    public Result<Page<AlbumVO>> queryPage(AlbumDTO albumDTO){
        Page<AlbumVO> pageResult = albumService.queryPage(albumDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/queryByThemeId")
    public Result<List<AlbumVO>> queryByThemeId(Integer themeId){
        List<AlbumVO> albunList = albumService.queryByThemeId(themeId);
        return Result.success(albunList);
    }

    @PostMapping("/updateStatus")
    public Result updateStatus(@RequestBody Album album){
        albumService.updateById(album);
        return Result.success();
    }

    @PostMapping("/addAlbum")
    public Result addAlbum(@RequestBody AlbumDTO album){
        albumService.addAlbum(album);
        return Result.success();
    }

    /**
     * 验证相册同名
     */
    @PostMapping("/sameAlbumName")
    public Result sameAlbumName(@RequestBody Album album){
        albumService.sameAlbumName(album);
        return Result.success();
    }

    @GetMapping("/queryById")
    public Result<Album> queryById(@RequestParam("albumId") Integer albumId){
        Album album = albumService.getById(albumId);
        return Result.success(album);
    }

    @PostMapping("/updateAlbum")
    public Result updateAlbum(@RequestBody Album album){
        albumService.updateById(album);
        return Result.success();
    }

    @PostMapping("/addPhotos")
    public Result addPhotos(@RequestBody AlbumDTO albumDTO){
        albumService.addPhotos(albumDTO);
        return Result.success();
    }

    @GetMapping("/queryAll")
    public Result<List<Album>> queryAll(){
        List<Album> list = albumService.lambdaQuery().list();
        return Result.success(list);
    }

    @GetMapping("/queryByName")
    public Result<List<Album>> queryByName(@RequestParam("name") String name){
        List<Album> list = albumService.lambdaQuery()
                .like(HxsUtils.isNotEmpty(name),Album::getName,name)
                .list();
        return Result.success(list);
    }

    @GetMapping("/changeShow")
    public Result changeShow(@RequestParam("flag") Integer flag){
        albumService.changeShow(flag);
        return Result.success();
    }
}
